import * as types from './mutations-types'
import {playMode} from 'common/js/config'
import {shuflle} from 'common/js/util'
import {saveSearches, deleteSearches, clearHistory} from 'common/js/cache'

function findSong (list, song) {
  return list.findIndex((item) => {
    return item.id === song.id
  })
}

// 歌曲列表点击播放
export const selectPlay = function ({commit, state}, {list, index}) {
  commit(types.SET_SEQUENCE_LIST, list)
  commit(types.SET_PLAY_LIST, list)
  commit(types.SET_CURRENT_INDEX, index)
  commit(types.SET_FULLSCREEN, true)
  commit(types.SET_PLAYING, true)
}
// 歌手背景图处随机播放
export const randomPlay = function ({commit, state}, {list, index}) {
  commit(types.SET_MODE, playMode.random)
  commit(types.SET_SEQUENCE_LIST, list)
  let randomList = shuflle(list)
  commit(types.SET_PLAY_LIST, randomList)
  commit(types.SET_CURRENT_INDEX, 0)
  commit(types.SET_FULLSCREEN, true)
  commit(types.SET_PLAYING, true)
}

// 点击搜索列表添加歌曲
export const insertSong = function ({commit, state}, song) {
  let playlist = state.playlist.slice()
  let sequenceList = state.sequenceList.slice()
  let currentIndex = state.currentIndex
  // 记录当前歌曲
  let currentSong = playlist[currentIndex]
  // 查找当前列表中是否有待插入的歌曲并返回其索引
  let fpIndex = findSong(playlist, song)
  // 因为是插入歌曲，所以索引+1
  currentIndex++
  // 插入这首歌到当前索引位置
  playlist.splice(currentIndex, 0, song)
  // 如果已经包含了这首歌
  if (fpIndex > -1) {
    // 如果当前插入的序号大于列表中的序号
    if (currentIndex > fpIndex) {
      playlist.splice(fpIndex, 1)
      currentIndex--
    } else {
      playlist.splice(fpIndex + 1, 1)
    }
  }

  let currentSIndex = findSong(sequenceList, currentSong) + 1

  let fsIndex = findSong(sequenceList, song)

  sequenceList.splice(currentSIndex, 0, song)

  if (fsIndex > -1) {
    if (currentSIndex > fsIndex) {
      sequenceList.splice(fsIndex, 1)
    } else {
      sequenceList.splice(fsIndex + 1, 1)
    }
  }

  commit(types.SET_PLAY_LIST, playlist)
  console.log(playlist)
  commit(types.SET_SEQUENCE_LIST, sequenceList)
  commit(types.SET_CURRENT_INDEX, currentIndex)
  commit(types.SET_FULLSCREEN, true)
  commit(types.SET_PLAYING, true)
}

// 保存搜索历史
export const saveHistory = function ({commit}, query) {
  commit(types.SET_SEARCH_HISTORY, saveSearches(query))
}
// 删除单个搜索历史
export const deleteHistory = function ({commit}, query) {
  commit(types.SET_SEARCH_HISTORY, deleteSearches(query))
}
// 清除播放列表
export const clearPlayList = function ({commit}) {
  commit(types.SET_CURRENT_INDEX, -1)
  commit(types.SET_PLAY_LIST, [])
  commit(types.SET_SEQUENCE_LIST, [])
  commit(types.SET_PLAYING, false)
  commit(types.SET_FULLSCREEN, false)
}

// 删除播放列表的单首歌曲
export function deleteSong ({commit, state}, song) {
  let playlist = state.playlist.slice()
  let sequenceList = state.sequenceList.slice()
  let currentIndex = state.currentIndex
  let pIndex = findSong(playlist, song)
  playlist.splice(pIndex, 1)
  let sIndex = findSong(sequenceList, song)
  sequenceList.splice(sIndex, 1)
  // 无法命中最后一首
  if (currentIndex > pIndex || currentIndex === playlist.length) {
    currentIndex--
  }

  commit(types.SET_PLAY_LIST, playlist)
  commit(types.SET_SEQUENCE_LIST, sequenceList)
  commit(types.SET_CURRENT_INDEX, currentIndex)
}

// 删除所有搜索历史
export const clearAll = function ({commit}) {
  commit(types.SET_SEARCH_HISTORY, clearHistory())
}
